<!doctype html>
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
  <meta charset="utf-8">
  <script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
  <script src="../../../web-component-tester/browser.js"></script>
  <link rel="import" href="../../polymer.html">
  <link rel="import" href="behaviors-elements.html">
<body>

<script>

suite('single behavior element', function() {

  var el;

  setup(function() {
    el = document.createElement('single-behavior');
    document.body.appendChild(el);
  });

  teardown(function() {
    document.body.removeChild(el);
  });

  test('ready from behavior', function() {
    assert.equal(el.__readyA, true);
  });

  test('properties from behavior', function() {
    el.label = 'foo';
    assert.equal(el.__label, 'foo');
  });

  test('listener from behavior', function() {
    el.fire('change', {value: 'bar'});
    assert.equal(el.__change, 'bar');
  });

  test('property info from behavior A', function() {
    assert.equal(el.getPropertyInfo('hasOptionsA').notify, true);
    assert.equal(el.getPropertyInfo('hasOptionsA').readOnly, true);
    assert.equal(typeof el._setHasOptionsA, 'function');
  });

  test('beforeRegister behavior', function() {
    if (Polymer.Settings.lazyRegister === 'max') {
      assert.isTrue(window.behaviorAsIsFail, 'Setting `is` in a behavior did not fail when lazyRegister: `max` set');
    } else {
      var el = document.createElement('behavior-as-is');
      assert.equal(el.is, 'behavior-as-is');
      assert.ok(el.$.content);
      el.prop = 42;
      assert.isTrue(el.propChanged1Called);
      assert.isTrue(el.propChanged2Called);
      assert.isTrue(el.hasAttribute('attr'));
    }
  });

});

suite('multi-behaviors element', function() {

  var el;

  setup(function() {
    var div = document.createElement('div');
    div.innerHTML = '<multi-behaviors user="user"></multi-behaviors>';
    el = div.firstElementChild;
    document.body.appendChild(el);
    CustomElements.takeRecords();
  });

  teardown(function() {
    document.body.removeChild(el);
  });

  test('ready from behaviors', function() {
    assert.equal(el.__readyA, true);
    assert.equal(el.__readyB, true);
  });

  test('properties from behaviors', function() {
    el.label = 'foo';
    assert.equal(el.__label, 'foo');
    el.disabled = true;
    assert.equal(el.__disabled, true);
  });

  test('properties from itself', function() {
    assert.isDefined(el._setFoo, 'readOnly setter not available');
    el._setFoo('bar');
    assert.equal(el.__foo, 'bar', 'observer not getting called');
    assert.equal(el.getAttribute('foo'), 'bar', 'not getting reflected');
  });

  test('listener from behaviors', function() {
    el.fire('change', {value: 'bar'});
    assert.equal(el.__change, 'bar');
  });

  test('property info from behavior A', function() {
    assert.equal(el.getPropertyInfo('hasOptionsA').notify, true);
    assert.equal(el.getPropertyInfo('hasOptionsA').readOnly, true);
    assert.equal(typeof el._setHasOptionsA, 'function');
  });

  test('property info from behavior B', function() {
    assert.equal(el.getPropertyInfo('hasOptionsB').readOnly, true);
    assert.equal(el.getPropertyInfo('hasOptionsB').notify, true);
    assert.equal(typeof el._setHasOptionsB, 'function');
  });

  test('multi-behavior overrides ordering', function() {
    assert.equal(el._toOverride, Polymer._toOverride, 'Behavior method was not overridden by prototype');
    assert(el.overridableProperty, 'Behavior property was not overridden by prototype');
    assert(el.overridablePropertyB, 'Behavior config-property was not overridden by sub-behavior');
  });

  test('hostAttributes ordering', function() {
    assert.equal(el.attributes.behavior.value, 'B', 'Behavior hostAttribute not overridden by subclass');
    assert.equal(el.attributes.user.value, 'user', 'Behavior hostAttribute overrode user attribute');
  });

  test('behavior is null generates warning', function() {
    var warned = false, oldWarn = Polymer.Base._warn;
    Polymer.Base._warn = function (message) {
      assert.match(message, /behavior is null/);
      warned = true;
    };
    Polymer({
      is: 'behavior-null',
      behaviors: [
        null
      ]
    });
    document.createElement('behavior-null');
    assert.equal(warned, true, 'Null behaviour should generate warning');
    Polymer.Base._warn = oldWarn;
  });

  test('behavior array is unique', function() {
    Polymer({
      is: 'behavior-unique',
      behaviors: [Polymer.BehaviorA, Polymer.BehaviorA]
    });
    assert.equal(document.createElement('behavior-unique').behaviors.length, 1);
  });

  test('duplicate behaviors keep last behavior', function() {
    Polymer({
      is: 'behavior-unique-last-behavior',
      behaviors: [[Polymer.BehaviorA, Polymer.BehaviorB], Polymer.BehaviorA]
    });
    var behaviors = document.createElement('behavior-unique-last-behavior').behaviors;
    assert.deepEqual(behaviors, [Polymer.BehaviorB, Polymer.BehaviorA]);
  });

});


suite('nested-behaviors element', function() {

  var el;

  setup(function() {
    el = document.createElement('nested-behaviors');
    document.body.appendChild(el);
  });

  teardown(function() {
    document.body.removeChild(el);
  });

  test('nested-behavior overrides ordering', function() {
    assert.ok(el.hasBehaviorA, "missing BehaviorA");
    assert.ok(el.hasBehaviorB, "missing BehaviorB");
    assert.ok(el.hasBehaviorC, "missing BehaviorC");
    assert.ok(el.hasBehaviorD, "missing BehaviorD");
    assert.equal(el._simpleProperty, 'D', 'Behavior simple property was not overridden by sub-behavior');
  });

});

</script>

</body>
</html>
